/**
 * ...
 * @author Hatshepsout
 */

function TextEditor(){}

TextEditor.prototype._sizeButton = null;
TextEditor.prototype._fontButton = null;
TextEditor.prototype._leftButton = null;
TextEditor.prototype._centerButton = null;
TextEditor.prototype._justifyButton = null;
TextEditor.prototype._rightButton = null;
TextEditor.prototype._italicButton = null;
TextEditor.prototype._colorPicker = null;
TextEditor.prototype._underlineButton = null;
TextEditor.prototype._container = null;
TextEditor.prototype._focusedTextField = null;
TextEditor.prototype._lastIndex = -1;

TextEditor.prototype.setCurrentTextField = function( textfield )
{
	this._focusedTextField = textfield;
	this.hide();
	
	if( this._focusedTextField != null )
	{
		this.display();
	}
};

TextEditor.prototype.init = function( elementId )
{
	this._container = document.getElementById(elementId);
	this._boldButton = document.getElementById('boldButton');
	this._italicButton = document.getElementById('italicButton');
	this._sizeButton = document.getElementById('sizeButton');
	this._fontButton = document.getElementById('fontButton');
	this._underlineButton = document.getElementById('underlineButton');
	this._leftButton = document.getElementById('leftButton');
	this._centerButton = document.getElementById('centerButton');
	this._justifyButton = document.getElementById('justifyButton');
	this._rightButton = document.getElementById('rightButton');
	this._colorPicker = new ColorPicker();
	this._colorPicker.init( document.getElementById("colorPickerContainer"), 146,159 );
	
	var scope = this;
	var callback = function(){
		scope._textEditHandler.apply(scope,arguments);
	};
	
	this._leftButton.onclick = callback;
	this._centerButton.onclick = callback;
	this._justifyButton.onclick = callback;
	this._rightButton.onclick = callback;
	this._boldButton.onclick = callback;
	this._italicButton.onclick = callback;
	this._underlineButton.onclick = callback;
	this._sizeButton.onchange = callback;
	this._fontButton.onchange = callback;
	this._colorPicker.addEventListener( ColorPicker.CHANGE_COLOR_EVENT, this, this._textEditHandler );
	
	this.refresh();
};

TextEditor.prototype.refresh = function()
{
	if( this._focusedTextField != null )
	{
		var index = this._focusedTextField.currentIndex;
		var textLength = this._focusedTextField.getText().length;
		var format = this._focusedTextField.defaultTextFormat;	
	
		if( textLength > 0 && index >= 0)
		{
			if( this._lastIndex != index )
			{
				format = this._focusedTextField.getTextFormat(index);
				this._colorPicker.setColor(format.textColor);
				this._sizeButton.value = parseInt(format.size);
				this._fontButton.value = format.font;
			}
		}
		else
		{
			this._sizeButton.value = parseInt(format.size);
			this._fontButton.value = format.font;
			this._colorPicker.setColor(format.textColor);
		}
			
		this._lastIndex = index;
	}
	
	setTimeout( this.refresh.bind(this), 500 );
};

TextEditor.prototype.display = function()
{
	//this._container.style.display = "block";
};

TextEditor.prototype.hide = function()
{
	//this._container.style.display = "none";
};

TextEditor.prototype._textEditHandler = function(event)
{
	if( this._focusedTextField == null )
		return;
		
	
	var range = this._focusedTextField.getSelectionRange();
	var start = range.start;
	var end = range.end;
	var currentFormat = null;
	var index = this._focusedTextField.currentIndex;
	
	
	switch( event.target )
	{
		case this._leftButton: this._focusedTextField.setTextAlign( tomahawk_ns.TextField.ALIGN_LEFT); break;
		case this._centerButton: this._focusedTextField.setTextAlign( tomahawk_ns.TextField.ALIGN_CENTER); break;
		case this._rightButton: this._focusedTextField.setTextAlign( tomahawk_ns.TextField.ALIGN_RIGHT); break;
		case this._justifyButton: this._focusedTextField.setTextAlign( tomahawk_ns.TextField.ALIGN_JUSTIFY); break;
	}
	
	if( start == -1 && end == -1 )
	{	
		currentFormat = this._focusedTextField.getTextFormat(index);
		
		switch( event.target )
		{
			case this._boldButton: currentFormat.bold = ( currentFormat.bold == true ) ? false : true; break;
			case this._italicButton: currentFormat.italic = ( currentFormat.italic == true ) ? false : true; break;
			case this._underlineButton: currentFormat.underline = ( currentFormat.underline == true ) ? false : true; break;
			case this._sizeButton: currentFormat.size = this._sizeButton.value; break;
			case this._fontButton: currentFormat.font = this._fontButton.value; break;
			case this._colorPicker: currentFormat.textColor = this._colorPicker.getColor(); break;
		}
		
		this._focusedTextField.setTextFormat(currentFormat,index,index);
		this._focusedTextField.defaultTextFormat = currentFormat.clone();
	}
	else
	{	
		for( ; start <= end; start++ )
		{
			currentFormat = this._focusedTextField.getTextFormat(start);
			
			switch( event.target )
			{
				case this._boldButton: currentFormat.bold = ( currentFormat.bold == true ) ? false : true; break;
				case this._italicButton: currentFormat.italic = ( currentFormat.italic == true ) ? false : true; break;
				case this._underlineButton: currentFormat.underline = ( currentFormat.underline == true ) ? false : true; break;
				case this._sizeButton: currentFormat.size = this._sizeButton.value; break;
				case this._fontButton: currentFormat.font = this._fontButton.value; break;
				case this._colorPicker: currentFormat.textColor = this._colorPicker.getColor(); break;
			}
			
			this._focusedTextField.setTextFormat(currentFormat,start,start);
		}
	}
};

